/**
 * @(#)MonitorContentTemplate.java 2017年2月16日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.common.monitor;


import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.annotation.ExportExcelAnnotation;
import org.jsbd.boss.domian.monitor.MonitorColor;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO 请加入本类的说明
 * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
 * @version 1.0.0
 */
public class MonitorContentTemplate {
	public static final Logger LOGGER = LoggerFactory.getLogger(MonitorContentTemplate.class);

	public static final String[][] MOBILE_TABLE_HEADER=new String[][]{{"应用名称","10%"},
		{"手机号获取成功率","10%"},{"新增任务数","10%"},{"手机号获取成功数","10%"},{"渠道名称","10%"},{"mark","20%"}};
		
	public static final String[][] ACTION_TABLE_HEADER=new String[][]{{"应用名称","10%"},{"文件上传失败率","10%"},
		{"获取任务总数","10%"},{"上传文件失败数","10%"},{"任务同步成功数","10%"},{"任务同步成功率","10%"},{"渠道名称","10%"},{"mark","20%"}};
		
	public static final String[][] NUM_TABLE_HEADER=new String[][]{{"应用名称","20%"},{"当前剩余任务量","10%"},
		{"新增任务数","10%"},{"留存任务数","10%"},{"已执行量","10%"},
		{"30分钟执行量(新增|留存)","10%"},{"已执行占比","10%"},{"提示","15%"}};
			
	public static final String[][] FLOW_TOTAL_HEADER=new String[][]{{"名称","25%"},{"流量值(MB)","15%"},
		{"统计点","25%"}};
			
	public static final String[][] FLOW_TIME_HEADER=new String[][]{{"时间","30%"},{"流量","20%"}};
	
	public static final String[][] FLOW_MARK_HEADER=new String[][]{{"应用名称","30%"},{"流量","20%"}};
			
	public static final String SIMULATOR_ALARM_INFO="<div style=\"font-size:80%;text-align:left;{color}\">{vername}版本：对应的模拟器至少还需要{needNum}个，实际在线模拟器为{onlineNum}个，空闲模拟器为:{unbusyNum}个</div>";
	
	public static final String COMMON_THEAD_TEMPLATE="<th width=\"{2}\" align=\"left\">{1}</th>";
	
    public static String getHtmlThead(String[][] headerArr){
    	StringBuffer thead=new StringBuffer();
		thead.append("<thead><tr>");
		for(String[] header:headerArr){
			thead.append(COMMON_THEAD_TEMPLATE.replace("{1}", header[0]).replace("{2}", header[1]));
		}
	    thead.append("</tr></thead>");
	    return thead.toString();
    }
	
    /**
     * 
     *将监控数据列表转换成table，用于邮件发送
     * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
     * @param dataList
     * @param headerArr
     * @return
     */
    public static String getHtmlTableInfo(List<?> dataList,String[][] headerArr){
    	StringBuffer buffer=new StringBuffer();
		buffer.append("<table border=\"1\">");
		buffer.append(getHtmlThead(headerArr));
		buffer.append("<tbody>");
		buffer.append(getTbodyTemplate(dataList));
		buffer.append("</tbody>");
		buffer.append("</table>");
		return buffer.toString();
    }
    
    /**
     * 
     * 将监控数据列表转换成String之后传递过来，用于邮件发送
     * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
     * @param content
     * @param headerArr
     * @return
     */
    public static String getHtmlTableInfo(String content,String[][] headerArr){
    	StringBuffer buffer=new StringBuffer();
		buffer.append("<table border=\"1\">");
		buffer.append(getHtmlThead(headerArr));
		buffer.append("<tbody>");
		buffer.append(content);
		buffer.append("</tbody>");
		buffer.append("</table>");
		return buffer.toString();
    }
	
    public static String getTbodyTemplate(int len){
		StringBuffer buffer=new StringBuffer();
		buffer.append("<tr>");
		for(int i=1;i<len+1;i++){
			buffer.append("<td>{"+i+"}</td>");
		}
		buffer.append("</tr>");
		return buffer.toString();
	}
    
    public static String getTbodyTemplate(List<?> dataList){
    	if(dataList==null||dataList.isEmpty()){
    		return "";
    	}
    	
    	StringBuffer buffer=new StringBuffer();
    	
    	for(Object data:dataList){
    		if(data instanceof MonitorColor){
    			MonitorColor obj=(MonitorColor)data;
    			if(StringUtils.isNotBlank(obj.getColour())){
    				buffer.append("<tr style=\"background-color:"+obj.getColour()+"\">");
    			}else{
    				buffer.append("<tr>");
    			}
	    	}else{
	    		buffer.append("<tr>");
	    	}
            
    		Field[] fields = data.getClass().getDeclaredFields();
    	    fields = getFieldsBySort(fields);
    	    int len = fields.length;
    	    for (int i = 0; i < len; i++) {
    	    	buffer.append("<td>"+getValueFromObj(data,fields[i])+"</td>");
    	    }
    	    buffer.append("</tr>");
    	}
		
		return buffer.toString();
	}
    
    /**
     * 
     * 反射从对象中获取值，并转换成String
     * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
     * @param data
     * @param field
     * @return
     */
    public static String getValueFromObj(Object data,Field field){
    	try{
    		field.setAccessible(true);
        	Object value = field.get(data);
        	if(value==null){
        		return "";
        	}
        	
        	if (value instanceof Date) {
        		return DateUtil.getYmdhmsDateStr((Date)value);
        	} else {
        		return value.toString();
        	}
    	}catch(Exception e){
    		LOGGER.error("",e);
    	}
    	return "";
    }
    
    private static Field[] getFieldsBySort(Field[] fields) {

        Map<Integer,Field> indexMap=new HashMap<Integer,Field>();
        List<Integer> annotationList = new LinkedList<Integer>();
        
        for (Field field : fields) {
            ExportExcelAnnotation ee = field.getAnnotation(ExportExcelAnnotation.class);
            if (ee != null) {
            	indexMap.put(ee.index(), field);
                annotationList.add(ee.index());
            }
        }
        
        Collections.sort(annotationList);
        
        Field[] fieldsArr = new Field[annotationList.size()];

        for (int i = 0; i < annotationList.size(); i++) {
        	fieldsArr[i]=indexMap.get(i+1);
        }
        
        return fieldsArr;
    }
    
}
